package com.example.benyq.mediaplayerdemo.recyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * @author benyq
 * @date 18-5-20 18:52
 * @E-mail： 1520063035@qq.com
 * @description
 */
public abstract class BenyqBaseAdapter<T> extends RecyclerView.Adapter<BenyqRVHolder> {

    protected Context mContext;
    protected List<T> mList;
    protected int layoutId;
    private OnItemChildClickListener mOnItemClickListener;

    public BenyqBaseAdapter(Context context, List<T> list, int layoutId) {
        mContext = context;
        mList = list;
        this.layoutId = layoutId;
    }


    @Override
    public BenyqRVHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(layoutId,parent,false);
        return new BenyqRVHolder(mContext, view);
    }

    @Override
    public void onBindViewHolder(final BenyqRVHolder holder, final int position) {
        if(mOnItemClickListener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onItemChildClick(holder.itemView,position);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    mOnItemClickListener.onItemLongClick(holder.itemView,position);
                    return false;
                }
            });
        }
        convert(holder, position, mList.get(position));
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public abstract void convert(BenyqRVHolder holder, int position, T t);

    public boolean addAll(List<T> list) {
        boolean result = mList.addAll(list);
        notifyDataSetChanged();
        return result;
    }

    public boolean addAll(int position, List list) {
        boolean result = mList.addAll(position, list);
        notifyDataSetChanged();
        return result;
    }

    public void add(T data) {
        mList.add(data);
        notifyDataSetChanged();
    }

    public void add(int position, T data) {
        mList.add(position, data);
        notifyDataSetChanged();
    }

    public void clear() {
        mList.clear();
        notifyDataSetChanged();
    }

    public boolean contains(T data) {
        return mList.contains(data);
    }

    public List<T> getData() {
        return mList;
    }

    public void addNewData(List<T> data){
        mList.clear();
        if (data == null){
            data = new ArrayList<>();
        }
        mList.addAll(data);
        notifyDataSetChanged();
    }

    public boolean remove(T data) {
        boolean result = mList.remove(data);
        notifyDataSetChanged();
        return result;
    }

    public void remove(int index) {
        mList.remove(index);
        notifyDataSetChanged();
    }

    public interface OnItemChildClickListener {
        /**
         * @param view     The view whihin the ItemView that was clicked
         * @param position The position of the view int the adapter
         */
        void onItemChildClick(View view, int position);
        void onItemLongClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemChildClickListener listener){
        this.mOnItemClickListener = listener;
    }

}
